package zhishuang.wang.sansi.x9.protocol;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 11:23
 * @Email 1406110602@qq.com
 */


import zhishuang.wang.sansi.fcms.protocol.ProtocolPackage;
import zhishuang.wang.sansi.x9.devinfor.DeviceInforX9Var;
import zhishuang.wang.sansi.x9.tcpservice.DeviceRealStatus;
import zhishuang.wang.sansi.x9.tcpservice.NettyX9Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class X9ProtocolDeal {
    public static DeviceRealStatus x9RealStatusPush = null;

    public X9ProtocolDeal() {
    }

    public static int x9OnlinStatusPush(String deviceId, int onlineFlag) {
        if (x9RealStatusPush != null) {
            x9RealStatusPush.deviceOnlineStatus(deviceId, onlineFlag);
        }

        return 0;
    }

    public static int sendDataToX9(byte[] data, int dataLen, String deviceId, int folNum, int cmd, int flag) {
        byte[] proBuff = ProtocolPackage.x9FacakgeToSend(data, dataLen, deviceId, folNum, cmd, flag);
        return proBuff != null ? NettyX9Service.sendMessageToX9(proBuff, deviceId) : -1;
    }

    public static int x9AutoTimeToDevice(String deviceId) {
        TimeZone zone = TimeZone.getTimeZone("GMT+00:00");
        Calendar cal = Calendar.getInstance(zone);
        int year = cal.get(1);
        int month = cal.get(2) + 1;
        int day = cal.get(5);
        int hour = cal.get(11);
        int minute = cal.get(12);
        int second = cal.get(13);
        int mMsecond = cal.get(14);
        byte[] yearByte = ProtocolPackage.addIntToByte(year, 2);
        byte[] mMsecondByte = ProtocolPackage.addIntToByte(mMsecond, 2);
        byte[] dataBuff = new byte[9];
        System.arraycopy(yearByte, 0, dataBuff, 0, 2);
        dataBuff[2] = (byte)(month & 255);
        dataBuff[3] = (byte)(day & 255);
        dataBuff[4] = (byte)(hour & 255);
        dataBuff[5] = (byte)(minute & 255);
        dataBuff[6] = (byte)(second & 255);
        System.arraycopy(mMsecondByte, 0, dataBuff, 7, 2);
        sendDataToX9(dataBuff, 9, deviceId, 1, 6, 1);
        return 0;
    }

    public static int x9AutoReadStatus(String deviceId) {
        byte[] dataBuff = new byte[]{3, 3, 5, 11};
        sendDataToX9(dataBuff, 4, deviceId, 1, 8, 1);
        return 0;
    }

    public static int x9BlockSizeSet(String deviceId) {
        byte[] blockSizeByte = ProtocolPackage.addIntToByte(DeviceInforX9Var.x9BlockSizeDeFault, 2);
        byte[] dataBuff = new byte[]{1, 0, 2, 0, 0};
        System.arraycopy(blockSizeByte, 0, dataBuff, 3, 2);
        sendDataToX9(dataBuff, 5, deviceId, 1, 24, 1);
        return 0;
    }

    private static int x9VersionDeal(byte[] data, String deviceId, int startP) {
        int size = data[startP] & 255;
        ++startP;
        String productVersion = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String softwareVersion = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String hardwareVersion = ProtocolPackage.getAcsiiStr(data, startP, size);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.deviceVersionInfor(deviceId, productVersion, softwareVersion, hardwareVersion);
        }

        return 0;
    }

    public static int x9LoginInDeal(byte[] data, String playIds, String deviceId, int folNum) {
        x9VersionDeal(data, deviceId, 13);
        int dataBuffLen = 0;
        int playIdsLen = playIds.length();
        byte[] dataBuff;
        if (playIds != null && playIdsLen != 0) {
            dataBuffLen = dataBuffLen + playIdsLen + 11;
            dataBuff = new byte[dataBuffLen];
            dataBuff[0] = 0;
            dataBuff[1] = 1;
            dataBuff[2] = (byte)(playIdsLen & 255);
            System.arraycopy(playIds.getBytes(), 0, dataBuff, 3, playIdsLen);
            dataBuff[3 + playIdsLen] = 0;
            dataBuff[4 + playIdsLen] = 0;
            dataBuff[5 + playIdsLen] = 0;
            dataBuff[6 + playIdsLen] = 0;
            dataBuff[7 + playIdsLen] = 0;
            dataBuff[8 + playIdsLen] = 0;
            dataBuff[9 + playIdsLen] = 0;
            dataBuff[10 + playIdsLen] = 0;
        } else {
            dataBuff = new byte[]{0};
        }

        sendDataToX9(dataBuff, dataBuffLen, deviceId, folNum, 129, 0);
        return 0;
    }

    public static int x9BeatDeal(String deviceId, int folNum) {
        byte[] dataBuff = new byte[]{0};
        sendDataToX9(dataBuff, 1, deviceId, folNum, 130, 0);
        return 0;
    }

    public static int x9SwitchDimControl(String deviceId, int folNum, int res, int flag) {
        String key = String.format("%s%05d", deviceId, folNum);
        Integer value = (Integer)DeviceInforX9Var.devX9ControlMap.get(key);
        if (value != null) {
            DeviceInforX9Var.devX9ControlMap.put(key, res);
        }

        return 0;
    }

    private static int x9TimeZoneDeal(byte[] data, String deviceId, int startP, int dataLen) {
        int size = data[startP] & 255;
        ++startP;
        String zoneId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String gmtOffset = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String zoneShort = ProtocolPackage.getAcsiiStr(data, startP, size);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.deviceTimeZone(deviceId, zoneId, gmtOffset, zoneShort);
        }

        return 0;
    }

    private static int x9TimeParamDeal(byte[] data, String deviceId, int startP, int dataLen) {
        int timeType = data[startP] & 255;
        String paramStr = "";
        if (timeType == 1) {
            int endP = startP + dataLen;
            startP += 2;
            byte[] patamByte = Arrays.copyOfRange(data, startP, endP);
            paramStr = new String(patamByte);
        }

        if (x9RealStatusPush != null) {
            x9RealStatusPush.deviceTimeParam(deviceId, timeType, paramStr);
        }

        return 0;
    }

    private static int environmentParamDeal(byte[] data, String deviceId, int startP, int dataLen) {
        if (dataLen > 3) {
            int sourceType = data[startP] & 255;
            int channel = data[startP + 1] & 255;
            int upload = data[startP + 2] & 255;
            int size = data[startP + 3] & 255;
            String source = "";
            size += 4;
            if (size == dataLen) {
                int endP = startP + dataLen;
                startP += 4;
                byte[] sourceByte = Arrays.copyOfRange(data, startP, endP);
                source = new String(sourceByte);
            }

            if (x9RealStatusPush != null) {
                x9RealStatusPush.environmentParam(deviceId, sourceType, channel, upload, source);
            }
        }

        return 0;
    }

    public static int x9StatusQueryDeal(byte[] data, String deviceId, int startP, int folNum, int returnFlag) {
        ++startP;
        int endP = data.length - 2;

        int statusLen;
        for(int i = startP; i < endP; i = i + 4 + statusLen) {
            int statusId = data[i] & 255;
            int supportFlag = data[i + 1] & 255;
            if (supportFlag == 1) {
                statusLen = ProtocolPackage.getIntVal(data, i + 2, 2);
                if (i + 4 + statusLen > endP) {
                    break;
                }

                switch (statusId) {
                    case 1:
                    case 6:
                    case 7:
                    case 9:
                    case 10:
                    case 12:
                    default:
                        break;
                    case 2:
                        x9VersionDeal(data, deviceId, i + 4);
                        break;
                    case 3:
                        if (x9RealStatusPush != null) {
                            x9RealStatusPush.deviceSwitchStaus(deviceId, data[i + 4] & 255);
                        }
                        break;
                    case 4:
                        x9TimeParamDeal(data, deviceId, i + 4, statusLen);
                        break;
                    case 5:
                        if (x9RealStatusPush != null) {
                            x9RealStatusPush.deviceDimmStatus(deviceId, data[i + 4] & 255);
                        }
                        break;
                    case 8:
                        x9TimeZoneDeal(data, deviceId, i + 4, statusLen);
                        break;
                    case 11:
                        int width = ProtocolPackage.getIntVal(data, i + 4, 2);
                        int hight = ProtocolPackage.getIntVal(data, i + 6, 2);
                        if (x9RealStatusPush != null) {
                            x9RealStatusPush.deviceResolution(deviceId, hight, width);
                        }
                        break;
                    case 13:
                        environmentParamDeal(data, deviceId, i + 4, statusLen);
                }
            } else {
                statusLen = 0;
            }
        }

        if (returnFlag == 1) {
            byte[] dataBuff = new byte[]{0};
            sendDataToX9(dataBuff, 1, deviceId, folNum, 137, 0);
        }

        return 0;
    }

    public static int x9PlayStatusQueryDeal(byte[] data, String deviceId, int startP, int folNum, int returnFlag) {
        int size = data[startP] & 255;
        ++startP;
        String playId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String nowPlanId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String nowProgramId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        ++startP;
        int dataLen = data.length - 2;
        List<Map> planList = new ArrayList();
        int i = startP;

        while(i < dataLen) {
            size = data[i] & 255;
            if (i + size + 17 > dataLen) {
                break;
            }

            ++i;
            String planId = ProtocolPackage.getAcsiiStr(data, i, size);
            i += size;
            long fileSize = ProtocolPackage.getLongVal(data, i, 8);
            i += 8;
            long downSize = ProtocolPackage.getLongVal(data, i, 8);
            i += 8;
            Map map = new HashMap();
            map.put("planId", planId);
            map.put("fileSize", fileSize);
            map.put("downSize", downSize);
            planList.add(map);
        }

        if (x9RealStatusPush != null) {
            x9RealStatusPush.devicePlayStatus(deviceId, playId, nowPlanId, nowPlanId, nowProgramId, planList);
        }

        if (returnFlag == 1) {
            byte[] dataBuff = new byte[]{0};
            sendDataToX9(dataBuff, 1, deviceId, folNum, 158, 0);
        }

        return 0;
    }

    public static int x9EnvironmentReqDeal(byte[] data, String deviceId, int startP, int folNum) {
        int dataLen = data.length - 2;
        int size = data[startP] & 255;
        ++startP;
        String source = ProtocolPackage.getAcsiiStr(data, startP, size);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.environmentDataReq(deviceId, source, folNum);
        }

        return 0;
    }

    public static int x9EnvironmentVarDeal(byte[] data, String deviceId, int startP, int folNum, int returnFlag) {
        ++startP;
        int dataLen = data.length - 2;
        String key = "";
        String value = "";
        Map<String, String> map = new HashMap();
        int i = startP;

        while(i < dataLen) {
            int size = data[i] & 255;
            ++i;
            key = ProtocolPackage.getAcsiiStr(data, i, size);
            i += size;
            int flag = data[i] & 255;
            ++i;
            size = data[i] & 255;
            ++i;
            value = ProtocolPackage.getAcsiiStr(data, i, size);
            i += size;
            map.put(key, value);
        }

        if (x9RealStatusPush != null) {
            x9RealStatusPush.environmentVar(deviceId, map);
        }

        if (returnFlag == 1) {
            byte[] dataBuff = new byte[]{0};
            sendDataToX9(dataBuff, 1, deviceId, folNum, 159, 0);
        }

        return 0;
    }

    public static int x9UpgradeStatusDeal(byte[] data, String deviceId, int startP, int folNum) {
        int size = data[startP] & 255;
        ++startP;
        int endP = startP + size;
        byte[] taskIdByte = Arrays.copyOfRange(data, startP, endP);
        String taskId = new String(taskIdByte);
        int percentage = data[endP];
        ++endP;
        int updataStatus = ProtocolPackage.getIntVal(data, endP, 4);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.deviceUpgradeStatus(deviceId, taskId, percentage, updataStatus);
        }

        byte[] dataBuff = new byte[]{0};
        sendDataToX9(dataBuff, 1, deviceId, folNum, 170, 0);
        return 0;
    }

    public static int x9FaultStatusUploadDeal(byte[] data, String deviceId, int startP, int folNum) {
        int dataLen = data.length;
        ++startP;
        dataLen -= 2;
        int value = 0;
        List<Map<String, Integer>> mapList = new ArrayList();

        int faultLen;
        for(int i = startP; i < dataLen; i = i + 3 + faultLen) {
            int type = data[i] & 255;
            faultLen = ProtocolPackage.getIntVal(data, i + 1, 2);
            switch (type) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 13:
                    value = data[i + 3] & 255;
                    break;
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                default:
                    type = 0;
            }

            if (type > 0) {
                Map<String, Integer> map = new HashMap();
                map.put("type", type);
                map.put("value", value);
                mapList.add(map);
            }
        }

        if (mapList.size() > 0 && x9RealStatusPush != null) {
            x9RealStatusPush.deviceFaultStatus(deviceId, mapList);
        }

        byte[] dataBuff = new byte[]{0};
        sendDataToX9(dataBuff, 1, deviceId, folNum, 138, 0);
        return 0;
    }

    public static int x9PlanExecStatusUploadDeal(byte[] data, String deviceId, int startP, int folNum) {
        int size = data[startP] & 255;
        ++startP;
        String planId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String itemId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        ProtocolPackage.timeX9ByteToString(data, startP, 9);
        startP += 9;
        String itemExecTime = ProtocolPackage.timeX9ByteToString(data, startP, 9);
        startP += 9;
        int ctrlType = data[startP];
        ++startP;
        int ctrlValue = data[startP];
        startP += 4;
        int execRes = data[startP];
        ++startP;
        int errCode = data[startP];
        ++startP;
        size = data[startP];
        ++startP;
        String errMessage = ProtocolPackage.getAcsiiStr(data, startP, size);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.devicePlanExecStatus(deviceId, planId, itemId, itemExecTime, ctrlType, ctrlValue, execRes, errCode, errMessage);
        }

        byte[] dataBuff = new byte[]{0};
        sendDataToX9(dataBuff, 1, deviceId, folNum, 172, 0);
        return 0;
    }

    public static int x9MaterialStatusUploadDeal(byte[] data, String deviceId, int startP, int folNum) {
        int size = data[startP] & 255;
        ++startP;
        String playId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        size = data[startP] & 255;
        ++startP;
        String materialId = ProtocolPackage.getAcsiiStr(data, startP, size);
        startP += size;
        String startTime = ProtocolPackage.timeX9ByteToString(data, startP, 9);
        startP += 9;
        String endTime = ProtocolPackage.timeX9ByteToString(data, startP, 9);
        if (x9RealStatusPush != null) {
            x9RealStatusPush.materialStatusUpload(deviceId, playId, materialId, startTime, endTime);
        }

        byte[] dataBuff = new byte[]{0};
        sendDataToX9(dataBuff, 1, deviceId, folNum, 157, 0);
        return 0;
    }
}

